package com.runnor.common.websys.system.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author runnor-yh
 * @date 2023年08月15日 11:24
 */
@Data
public class ApiResult implements Serializable {
    private Integer code;
    private String message;
    private Object data;

    private String t;

    public ApiResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ApiResult ok(Object data) {
        return new ApiResult(200, "操作成功") {{
            this.setData(data);
        }};
    }

    public static ApiResult notFound() {
        return new ApiResult(404, "资源不存在");
    }

    public static ApiResult notPermit() {
        return new ApiResult(403, "无权限");
    }

    public static ApiResult notAuth() {
        return new ApiResult(401, "未登录");
    }

    public static ApiResult badRequest() {
        return new ApiResult(400, "操作未处理");
    }

    public static ApiResult error(Exception ex) {
        return new ApiResult(500, "系统繁忙") {{
            this.setData(ex.getLocalizedMessage());
        }};
    }
}
